package com.example.shaketounlock;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	
	private ToggleButton tbShake, tbProximity,tbAutoStart;
	private SeekBar sbHold;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Register();
		tbShake = (ToggleButton) findViewById(R.id.tbShake);
		tbProximity = (ToggleButton) findViewById(R.id.tbProximity);
		tbAutoStart= (ToggleButton) findViewById(R.id.tbAutoStart);
		sbHold  = (SeekBar) findViewById(R.id.sbHold);
		// khai bao Intent de start Service tuong tu start Activity

		tbShake.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent mIntent = new Intent(MainActivity.this, MyService.class);
				if (tbShake.isChecked()) {
					// Neu nut dang la ON
					startService(mIntent);
				} else {
					// Neu nut dang la OFF
					stopService(mIntent);
				}
			}
		});
		tbProximity.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent Proximity = new Intent(MainActivity.this, ProximitySensor.class);
				if (tbProximity.isChecked()) {
					// Neu nut dang la ON
					startService(Proximity);
					
				} else {
					// Neu nut dang la OFF
					stopService(Proximity);
					
					
				}
			}
		});
		tbAutoStart.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			
				if (tbAutoStart.isChecked()) {
					enableBroadcastReceiver();
				} else {
					disableBroadcastReceiver();
				}
			}
		});
		sbHold.setProgress((int)(SettingData.threshold));
		sbHold.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       

		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       

		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
		        // TODO Auto-generated method stub      
		    	if(progress<10)
		    		progress=10;
		       SettingData.threshold=progress;

		    
		    }       
		});
		SettingData.deviceManger = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
		SettingData.activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		SettingData.compName = new ComponentName(this, MyAdminReceiver.class);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void Register() {
		Intent intent1 = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
				SettingData.compName);
		intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
				"Additional text explaining why this needs to be added.");
		startActivityForResult(intent1, SettingData.RESULT_ENABLE);
	}
	/**
     * This method enables the Broadcast receiver registered in the AndroidManifest file.
     * @param view
     */
	public void enableBroadcastReceiver(){
	    ComponentName receiver = new ComponentName(this, ManagerBroadcastReceiver.class);
	    PackageManager pm = this.getPackageManager();

	    pm.setComponentEnabledSetting(receiver,
	            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
	            PackageManager.DONT_KILL_APP);
	    //Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
	   }
	   /**
	    * This method disables the Broadcast receiver registered in the AndroidManifest file.
	    * @param view
	    */
	   public void disableBroadcastReceiver(){
	    ComponentName receiver = new ComponentName(this, ManagerBroadcastReceiver.class);
	    PackageManager pm = this.getPackageManager();

	    pm.setComponentEnabledSetting(receiver,
	            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
	            PackageManager.DONT_KILL_APP);
	    //Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
	   }   

}
